<template>
  <div class="risk">
    <dialog-page-list
      title="选择质量检查表"
      confirmButton="确定"
      :show.sync="dialogShow"
      :url="url"
      :params="params"
      ref="page"
      @callback="submit"
      @initList="_initList"
    >
      <van-cell
        v-for="(dataItem, index) in list"
        :key="index"
        @click="selectItem(dataItem)"
      >
        <div class="title">{{ dataItem.Title }}</div>
        <van-row>
          <van-col span="12">
            <img
              src="@/assets/Images/AppIcon/icon_list_kind.png"
              class="icon"
            />{{ dataItem.CheckTypeName }}</van-col
          >
          <van-col span="12">
            <img
              src="@/assets/Images/AppIcon/icon_list_type.png"
              class="icon"
            />{{ dataItem.CheckFrequencyName }}</van-col
          >
        </van-row>
        <div class="remarks">
          {{ dataItem.Remarks }}
        </div>

        <template #right-icon v-if="isMulti != 'f'">
          <van-checkbox :value="isSelected(dataItem)" />
        </template>
      </van-cell>
    </dialog-page-list>
  </div>
</template>

<script>
import DialogPageList from "@/components/DialogPageList.vue";

export default {
  components: {
    DialogPageList,
  },
  props: {
    show: {},
    isMulti: {},
  },
  data() {
    return {
      url: "/SYQualityTemplate/GetPageData",
      list: [],
      selected: [],
      params: [
        {
          dataType: "CompanyList",
        },
      ],
    };
  },
  computed: {
    dialogShow: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit("update:show", value);
      },
    },
  },
  methods: {
    _initList(list) {
      this.list = list;
    },
    submit() {
      var tCodes = [];
      var tNames = [];
      for (var i = 0; i < this.selected.length; i++) {
        tCodes.push(this.selected[i].TCode);
        tNames.push(this.selected[i].Title);
      }
      var data = {
        TCodes:tCodes.join(","),
        TNames:tNames.join(",")
      }
      this.$emit("callback", data);
    },
    selectItem(item) {
      let tCode = item.TCode;
      if (this.isMulti == false) {
        this.selected = [];
      }
      let flag = false;
      for (var i = 0; i < this.selected.length; i++) {
        if (this.selected[i].TCode == tCode) {
          flag = true;
          this.selected.splice(i, 1);
          break;
        }
      }
      if (!flag) {
        this.selected.push(item);
      }
      if (this.isMulti == false) {
        this.callback(true);
      }
    },
    isSelected(item) {
      let tCode = item.TCode;
      let flag = false;
      for (var i = 0; i < this.selected.length; i++) {
        if (this.selected[i].TCode == tCode) {
          flag = true;
          break;
        }
      }
      return flag;
    },
  },
};
</script>